package com.sktlab.bizconfmobile.model;

import org.apache.mina.core.session.IoSession;

import com.sktlab.bizconfmobile.mina.MinaMsg;
import com.sktlab.bizconfmobile.mina.MinaUtil;
import com.sktlab.bizconfmobile.model.manager.CommunicationManager;
import com.sktlab.bizconfmobile.util.Util;

public class ACCSession extends BaseBusinessSession {

	public static final String TAG = "ACCSession";

	@Override
	public void create(IoSession session, LSSession ls) {
		
		MinaMsg msg = new MinaMsg();
		
		msg.clearMsgData();
		msg.setSsnId(ls.getId());
		msg.setSeq(ls.getSeqNum());
		msg.setMsgId(MinaUtil.MSG_LS_CS);		
		msg.appendMsgData(MinaUtil.MSG_ACC);
		
		session.write(msg.toString());
	}
	
	/**
	 * 
	 * @param session
	 * @param conf
	 */
	public void assocWithConf(IoSession session, Conference conf) {
			
		sendMsg(session,  
				MinaUtil.MSG_ASSOC_CONF,
				conf.getAttr().getConfId());
	}
	
	public void muteParty(IoSession session,Participant party, int playMessage){
		
		sendMsg(session, 
				MinaUtil.MSG_MUTE_PARTY,
				String.valueOf(playMessage),
				"0",
				party.getIdInConference());
	}
	/**
	 * 
	 * @param session
	 * @param state 0 stop 1 start
	 */
	public void record(IoSession session, int state) {
		
		sendMsg(session, 
				MinaUtil.MSG_RECORD,
				String.valueOf(state));
	}
	/**
	 * roll call party's name
	 * @param session
	 */
	public void rollCall(IoSession session) {
	
		String confId = ConfControl.getInstance().getActiveConference().getAttr().getConfId();
		
		Util.BIZ_CONF_DEBUG(TAG, "conf id: " + confId);
		
		sendMsg(session, MinaUtil.MSG_ROLL_CALL,confId,"0");
	}

	/**
	 * out call a guest user
	 * 
	 * @param party
	 */
	public void guestOutCallParty(IoSession session, Participant party) {
		
		String outCallPartyId = party.getIdInConference();
		
		Util.BIZ_CONF_DEBUG(TAG,"out call party id: " + outCallPartyId);
		
		sendMsg(session, MinaUtil.MSG_GUEST_OUT_CALL,outCallPartyId, "0");
	}

	/**
	 * call moderator party
	 * 
	 * @param party
	 */
	public void moderatorOutCall(IoSession session, Participant party) {
		
		String outCallPartyId = party.getIdInConference();
		
		Util.BIZ_CONF_DEBUG(TAG,"out call party id: " + outCallPartyId);
		
		sendMsg(session, MinaUtil.MSG_OUT_CALL,outCallPartyId);
	}

	/**
	 * active a conference whose conference account is the user clicked
	 * 
	 * The conference is based on the user clicked conference account
	 */
	public void activeConf(IoSession session, LSSession lsSession) {

		ConferenceAccount account = CommunicationManager.getInstance()
				.getActiveAccount();
		String moderatorPw = account.getModeratorPw();
		// see the api doc for more information
		String startImmediately = "0";
		String partition = "0";
		
		sendMsg(session, MinaUtil.MSG_ACTIVATE_CONF,
				lsSession.getBridgeNumber(),
				moderatorPw,
				startImmediately,
				partition);
		
		ConfControl.notifyObj();
	}

	/**
	 * add party to current active conference which start by this app
	 * 
	 * @param party
	 * @param isModerator
	 */
	public void addPartyToConf(IoSession session, Participant party,
			boolean isModerator) {

		addPartyToConf(session, party, isModerator, ConfControl
				.getInstance().getActiveConference().getAttr().getConfId());
	}

	/**
	 * add a party to the specified conference
	 * 
	 * @param party
	 * @param isModerator
	 * @param confId
	 */
	public void addPartyToConf(IoSession session, Participant party,
			boolean isModerator, String confId) {

		String host = "0";

		if (isModerator) {

			host = "1";
		}

		sendMsg(session, MinaUtil.MSG_CREATE_PARTY,
				confId,
				party.getName(),
				Util.getFormatPhoneNum(party.getPhone()),
				host,
				"~~~~~~");
	}

	/**
	 * erase conference in bridge
	 * 
	 * @param session
	 *            link session
	 * @param conf
	 *            conference to destory
	 */
	public void destoryConf(IoSession session, Conference conf) {
		
		sendMsg(session, MinaUtil.MSG_CONF_DESTORY,
					conf.getAttr().getConfId(),
					"0");
	}

	/**
	 * disconnect a party with the conference
	 * 
	 * @param session
	 *            link session with server
	 * @param party
	 *            party to be disconnected
	 */
	public void disconnectParty(IoSession session, Participant party) {
		
		String forceDisconnect = "1";
		
		sendMsg(session, MinaUtil.MSG_DISCONNECT_PARTY,
					party.getIdInConference(),
					forceDisconnect);
	}
	
	public void lock(IoSession session, Conference conf,int state) {
		
		sendMsg(session, MinaUtil.MSG_LOCK,
						conf.getAttr().getConfId(),
						"ConfSecure",
						String.valueOf(state));
	}
	
	public void confMute(IoSession session,int muteState, int muteParticipants){
		
		sendMsg(session, 
					MinaUtil.MSG_CONF_MUTE,
					String.valueOf(muteState),
					String.valueOf(muteParticipants));
	}
}
